//
//  SJTimeSelectDatePickerView.m
//  Init
//
//  Created by zhaoshijie on 24/01/2018.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "SJTimeSelectDatePickerView.h"
#import "SJColorConvertTool.h"

@implementation SJTimeSelectDatePickerView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {

        //  初始化视图
        [self initView];
        
    }
    return self;
}


#pragma mark ----------初始化视图----------
//  初始化视图
- (void)initView
{
    self.backgroundColor = [SJColorConvertTool colorWithHexString:@"000000" alpha:0.5];
    
    //  创建控件
    [self createViewInBgView];
    
    //  创建控件    创建标题的背景试图
    [self createViewInTitleBgView];
    
    //  创建控件    创建标题、取消、确定按钮
    [self createViewInTitleAndButton];
    
    //  创建控件    时间选择
    [self createViewInTimeSelectDatePicker];
    
    //  创建手势
    [self createInTap];
}

//  创建控件    
- (void)createViewInBgView
{
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-256, [UIScreen mainScreen].bounds.size.width, 256)];
    self.bgView.backgroundColor = [SJColorConvertTool colorWithHexString:@"f7f7f7"];
    [self addSubview:self.bgView];
}

//  创建控件    创建标题的背景试图
- (void)createViewInTitleBgView
{
    self.titleBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
    self.titleBgView.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:self.titleBgView];
}

//  创建控件    创建标题、取消、确定按钮
- (void)createViewInTitleAndButton
{
    //  取消
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.frame = CGRectMake(0, 0, 80, 40);
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[SJColorConvertTool colorWithHexString:@"666666"] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.titleBgView addSubview:self.cancelButton];
    
    //  标题
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, [UIScreen mainScreen].bounds.size.width-80*2, 40)];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [SJColorConvertTool colorWithHexString:@"333333"];
    self.titleLabel.text = @"时间选择";
    [self.titleBgView addSubview:self.titleLabel];
    
    //  确定
    self.sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sureButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-80, 0, 80, 40);
    self.sureButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureButton setTitleColor:[SJColorConvertTool colorWithHexString:@"666666"] forState:UIControlStateNormal];
    [self.sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.titleBgView addSubview:self.sureButton];
    
    
    //  分割线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 40-0.5, [UIScreen mainScreen].bounds.size.width, 0.5)];
    lineView.backgroundColor = [SJColorConvertTool colorWithHexString:@"ededed"];
    [self.titleBgView addSubview:lineView];
    
}


//  创建控件    时间选择
- (void)createViewInTimeSelectDatePicker
{
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 216)];
    self.datePicker.datePickerMode = UIDatePickerModeDate ;
    self.datePicker.backgroundColor = [SJColorConvertTool colorWithHexString:@"f7f7f7"];
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.locale = locale;
    [self.datePicker setValue:[SJColorConvertTool colorWithHexString:@"111111"] forKey:@"textColor"];
    
    [self.bgView addSubview:self.datePicker];
}


//  创建手势
- (void)createInTap
{
    UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGClick:)];
    [self addGestureRecognizer:tapG];
}

#pragma mark --------------方法--------------


/**
 添加的方法
 */
- (void)addViewToKeyWindow
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}


/**
 设置当前时间

 @param timeStamp 当前的时间戳
 */
- (void)settingDatePickerWithmNewTimeStamp:(long)timeStamp
{
    self.datePicker.date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
}


/**
 设置最大时间

 @param timeStamp 最大的时间戳
 */
- (void)settingDatePickerWithmMaxTimeStamp:(long)timeStamp
{
    self.datePicker.maximumDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];
}


/**
 设置最小时间

 @param timeStamp 最小的时间戳
 */
- (void)settingDatePickerWithmMinTimeStamp:(long)timeStamp
{
    self.datePicker.minimumDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];
}



#pragma mark --------------响应方法--------------
//  点击空白处移除试图
- (void)tapGClick:(UITapGestureRecognizer *)tap
{
    //  移除
    [self removeFromSuperview];
}


//  取消
- (void)cancelButtonClick:(UIButton *)sender
{
    //  移除
    [self removeFromSuperview];
}

//  确定
- (void)sureButtonClick:(UIButton *)sender
{
    //  移除
    [self removeFromSuperview];
    
    if(self.sureBlock)
    {
        long timeStamp = [self.datePicker.date timeIntervalSince1970];
        self.sureBlock(timeStamp);
    }
}

@end
